

#include "mapping/2d/probability_grid_range_data_inserter.h"

#include <cstdlib>

#include "Eigen/Core"
#include "Eigen/Geometry"
#include "mapping/2d/xy_index.h"
#include "mapping/internal/2d/ray_casting.h"
#include "mapping/probability_values.h"

namespace cartographer {
namespace mapping {

ProbabilityGridRangeDataInserter2D::ProbabilityGridRangeDataInserter2D(
    const ProbabilityGridRangeDataInserterOptions2D &options)
    : options_(options)
    , hit_table_(ComputeLookupTableToApplyCorrespondenceCostOdds(Odds(options.hit_probability)))
    , miss_table_(ComputeLookupTableToApplyCorrespondenceCostOdds(Odds(options.miss_probability))) {}

/*
 * 调用RayCast算法进行地图更新
 */
void ProbabilityGridRangeDataInserter2D::Insert(const sensor::RangeData &range_data,
                                                ProbabilityGrid *const   probability_grid) const {

    // By not finishing the update after hits are inserted, we give hits priority
    // (i.e. no hits will be ignored because of a miss in the same cell).
    CastRays(range_data, hit_table_, miss_table_, options_.insert_free_space, probability_grid);
    probability_grid->FinishUpdate();
}

}  // namespace mapping
}  // namespace cartographer
